﻿using LxBasic;


namespace LxCore
{
    public class IntegralStatistic
    {
        public class Base : StatisticBase
        {
 
        }
        public class Sum : Base
        {
            public override AccumulationDouble RunReport(UserMonitor userMonitor, bool isConsole = false)
            {
                AccumulationDouble total = new AccumulationDouble();
                if (isConsole)
                {

                }
                foreach ((FvInner z, ICellVarArr<double> zf, FvBound b, IFaceVarArr<double> bf) in getSelected(userMonitor, true, true))
                {
                    AccumulationDouble local = new AccumulationDouble();
                    if (z != null)
                    {
                        if (zf != null)
                        {
                            MultiThreads.RunParallelTask((tid) =>
                            {
                                Float128 tempSum = 0;
                                (int i1, int endp1) = z.CellIdx.Split(tid);
                                for (; i1 < endp1; i1++)
                                {
                                    tempSum += zf.GetCellVar(i1);
                                }
                                ThreadsMath.MemoryAdd(tempSum);
                            });
                            (double sum1, _, _) = ThreadsMath.MemoryRecallClear();
                            local += sum1;
                        }
                        if (isConsole)
                        {

                        }
                    }
                    else if (b != null)
                    {
                        if (bf != null)
                        {
                            MultiThreads.RunParallelTask((tid) =>
                            {
                                Float128 tempSum = 0;
                                (int i1, int endp1) = b.FaceIdx.Split(tid);
                                for (; i1 < endp1; i1++)
                                {
                                    tempSum += bf.GetFaceVar(i1);
                                }
                                ThreadsMath.MemoryAdd(tempSum);
                            });
                            (double sum1, _, _) = ThreadsMath.MemoryRecallClear();
                            local += sum1;
                        }
                        if (isConsole)
                        {

                        }
                    }
                    total += local;
                }
                if (isConsole)
                {
                    //输出
                }
                return total;
            }
 
        }
        public class VolInt : Base
        {
            public override AccumulationDouble RunReport(UserMonitor userMonitor, bool isConsole = false)
            {
                AccumulationDouble total = new AccumulationDouble();
                if (isConsole)
                {

                }
                foreach ((FvInner z, ICellVarArr<double> zf, FvBound b, IFaceVarArr<double> bf) in getSelected(userMonitor, true, false))
                {
                    AccumulationDouble local = new AccumulationDouble();
                    if (z != null)
                    {
                        if (zf != null)
                        {
                            MultiThreads.RunParallelTask((tid) =>
                            {
                                Float128 tempSum = 0;
                                (int i1, int endp1) = z.CellIdx.Split(tid);
                                FvInnCell[] cells = z.InZone.InnCells; 
                                for (; i1 < endp1; i1++)
                                {
                                    tempSum += zf.GetCellVar(i1) * cells[i1].Volumn;
                                }
                                ThreadsMath.MemoryAdd(tempSum);
                            });
                            (double sum1, _, _) = ThreadsMath.MemoryRecallClear();
                            local += sum1;
                        }
                        if (isConsole)
                        {

                        }
                    }
                    total += local;
                }
                if (isConsole)
                {
                    //输出
                }
                return total;
            }
        }
        public class AreaInt : Base
        {
            public override AccumulationDouble RunReport(UserMonitor userMonitor, bool isConsole = false)
            {
                AccumulationDouble total = new AccumulationDouble();
                if (isConsole)
                {

                }
                foreach ((FvInner z, ICellVarArr<double> zf, FvBound b, IFaceVarArr<double> bf) in getSelected(userMonitor, false, true))
                {
                    AccumulationDouble local = new AccumulationDouble();
                    if (b != null)
                    {
                        if (bf != null)
                        {
                            MultiThreads.RunParallelTask((tid) =>
                            {
                                Float128 tempSum = new();
                                (int i1, int endp1) = b.FaceIdx.Split(tid);
                                var faces = b.InZone.BouFaces; 
                                for (; i1 < endp1; i1++)
                                {
                                    tempSum += bf.GetFaceVar(i1) * faces[i1].Area;
                                }
                                ThreadsMath.MemoryAdd(tempSum);
                            });
                            (double sum1, _, _) = ThreadsMath.MemoryRecallClear();
                            local += sum1;
                        }
                        if (isConsole)
                        {

                        }
                    }
                    total += local;
                }
                if (isConsole)
                {
                    //输出
                }
                return total;
            }

        }
        public class FieldInt : Base
        {

        }
    }
}
//public override void RunStatistic(bool IsConsole)
//{
//    base.RunStatistic(IsConsole);
//    StatisticZoneThreads(IsConsole);
//}
//public override void StatisticCells(IIndexHost cellIdx, ICellVar<double> sf, ICellVar<double> assistant, ThreadInfo threadInfo)
//{
//    Float128 sum = new();
//    (int i1, int endp1) = cellIdx.GetCellIdx().GetTaskIdx(threadInfo);
//    for (; i1 < endp1; i1++)
//    {
//        sum += (sf.GetCellValue(i1));
//    }
//    ThreadsMath.MemoryAdd(sum);
//}
//public override void StatisticFaces(IIndexHost faceIdx, IFaceVar<double> sf, IFaceVar<double> assistant, ThreadInfo threadInfo)
//{
//    Float128 sum = new();
//    (int i1, int endp1) = faceIdx.GetFaceIdx().GetTaskIdx(threadInfo);
//    for (; i1 < endp1; i1++)
//    {
//        sum += (sf.GetFaceValue(i1));
//    }
//    ThreadsMath.MemoryAdd(sum);
//}
//public override void StatisticCells(IIndexHost cellIdx, ICellVar<double> sf, ICellVar<double> assistant, ThreadInfo threadInfo)
//{
//    Float128 sum = new();
//    (int i1, int endp1) = cellIdx.GetCellIdx().GetTaskIdx(threadInfo);
//    for (; i1 < endp1; i1++)
//    {
//        sum += sf.GetCellValue(i1) * assistant.GetCellValue(i1);
//    }
//    ThreadsMath.MemoryAdd(sum);
//}
//public override void StatisticFaces(IIndexHost faceIdx, IFaceVar<double> sf, IFaceVar<double> assistant, ThreadInfo threadInfo)
//{
//    Float128 sum = new();
//    (int i1, int endp1) = faceIdx.GetFaceIdx().GetTaskIdx(threadInfo);
//    for (; i1 < endp1; i1++)
//    {
//        sum += sf.GetFaceValue(i1) * assistant.GetFaceValue(i1);
//    }
//    ThreadsMath.MemoryAdd(sum);
//}
//public override void LocalFinish()
//{
//    (LocalResult, double s2, double s3) = ThreadsMath.MemoryRecallClear();
//    GlobalResult += LocalResult;
//}
//public override void RunStatistic(bool IsConsole)
//{
//    InMonitor.CurrentResult = 0;
//}
//protected override void SortOutResult()
//{
//    (double TempResult, double s2, double s3) = ThreadsMath.MemoryRecallClear();
//    InMonitor.CurrentResult += TempResult;
//}
//protected override void StatisticZone(FvZone zone, IField<double> scalar, ThreadInfo threadInfo)
//{
//    Float128 sum = new();
//    (int i1, int endp1) = zone.Inner.CellIdx.GetSeg(threadInfo);
//    var sf = scalar.GetCellField(zone.Inner, this);
//    for (; i1 < endp1; i1++)
//    {
//        sum += (sf.GetCellValue(i1));
//    }
//    ThreadsMath.MemoryAdd(sum);
//}
//protected override void StatisticThread(FvZone zone, FvBound thread, IField<double> scalar, ThreadInfo threadInfo)
//{
//    Float128 sum = new();
//    (int i1, int endp1) = thread.CellIdx.GetSeg(threadInfo);
//    var sf = scalar.GetFaceField(thread, this);
//    for (; i1 < endp1; i1++)
//    {
//        sum += (sf.GetFaceValue(i1));
//    }
//    ThreadsMath.MemoryAdd(sum);
//}
//public class Volumn : Base
//{
//    //public override void RunStatistic(bool IsConsole)
//    //{
//    //    base.RunStatistic(IsConsole);
//    //    StatisticZones(IsConsole);
//    //}
//    protected override void StatisticZone(FvZone zone, IField<double> scalar, ThreadInfo threadInfo)
//    {
//        Float128 sum = new();
//        var cells = zone.Cells;
//        (int i1, int endp1) = zone.Inner.CellIdx.GetSeg(threadInfo);
//        var sf = scalar.GetCellField(zone.Inner, this);
//        for (; i1 < endp1; i1++)
//        {
//            sum += (sf.GetCellValue(i1) * cells[i1].Volumn);
//        }
//        ThreadsMath.MemoryAdd(sum);
//    }
//}
//public class Area : Base
//{
//    //public override void RunStatistic(bool IsConsole)
//    //{
//    //    base.RunStatistic(IsConsole);
//    //    StatisticThreads(IsConsole);
//    //}
//    protected override void StatisticThread(FvZone zone, FvBound thread, IField<double> scalar, ThreadInfo threadInfo)
//    {
//        Float128 sum = new();
//        var faces = zone.Faces;
//        (int i1, int endp1) = thread.FaceIdx.GetSeg(threadInfo);
//        var sf = scalar.GetFaceField(thread, this);
//        for (; i1 < endp1; i1++)
//        {
//            sum += (sf.GetFaceValue(i1) * faces[i1].Area);
//        }
//        ThreadsMath.MemoryAdd(sum);
//    }
//}